﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Xml;

namespace GoShopping.Web.ProductManager
{
    public partial class ProductList1 : System.Web.UI.Page
    {

        BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //Master.ElementSelectedChange = GetTreeView;
                GetGVData(null);
                SetDDLSearch();
            }
        }

        private void SetDDLSearch()
        {
            ListItem li1 = new ListItem("-请选择-", "All");
            ListItem li2 = new ListItem("商品名", "Name");
            ListItem li3 = new ListItem("商品类型", "ProductTypeID");
            ListItem li4 = new ListItem("供应商", "SupplyerID");
            //ListItem li5 = new ListItem("上架?", "IsShowOnSale");
            ddlSearchCondition.Items.Add(li1);
            ddlSearchCondition.Items.Add(li2);
            ddlSearchCondition.Items.Add(li3);
            ddlSearchCondition.Items.Add(li4);
            //ddlSearchCondition.Items.Add(li5);
        }

        //private void GetTreeView()
        //{
        //    TreeView tv = Master.MasterTreeView;
        //    SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
        //}

        protected void btnInput_Click(object sender, EventArgs e)
        {
           Response.Redirect("ProductIn.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            switch (ddlSearchCondition.SelectedValue)
            {
                case "All":
                    {
                        GetGVData(null);
                    } break;
                case "Name":
                    {
                        string filterString = ddlSearchCondition.SelectedValue + " LIKE '%" + zltxtSearchValue.Text.Trim() + "%'";
                        GetGVData(filterString);
                    } break;
                case "ProductTypeID": 
                    {
                        string filterString = ddlSearchCondition.SelectedValue + " IN (SELECT ID FROM ProductType WHERE ParentID != -1 AND TypeName LIKE '%" + zltxtSearchValue.Text.Trim() + "%')";
                        GetGVData(filterString);
                    } break;
                case "SupplyerID":
                    {
                        string filterString = ddlSearchCondition.SelectedValue + " IN (SELECT ID FROM PrdctSupplyer WHERE Name LIKE '%" + zltxtSearchValue.Text.Trim() + "%')";
                        GetGVData(filterString);
                    } break;
                default: break;
            }            
        }

        protected void lbtnAdvanceSearch_Click(object sender, EventArgs e)
        {
            
        }

        void GetGVData(string filterString)
        {
            if (string.IsNullOrEmpty(filterString))
            {
                //IList<Model.Product> listPrdt = pb.GetAllProduct();
                IList<Model.Product> listPrdt = pb.GetProductList(" AND IsShowOnSale='true'");
                extGVProduct.DataSource = listPrdt;
                extGVProduct.DataBind();
            }
            else
            {
                IList<Model.Product> listPrdt = pb.GetProductList(" AND IsShowOnSale='true' AND " + filterString);
                extGVProduct.DataSource = listPrdt;
                extGVProduct.DataBind();
            }
        }

        protected void btnAddProduct_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddProducts.aspx"); 
        }

        protected void btnRecycle_Click(object sender, EventArgs e)
        {
            Response.Redirect("DeletedProduct.aspx");
        }

        protected void btnManage_Click(object sender, EventArgs e)
        {
            Response.Redirect("TagManage.aspx");
        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            ArrayList selList = new ArrayList();
            int i = 0;
            // 遍历数据集
            foreach (GridViewRow row in this.extGVProduct.Rows)
            {
                // 获取行首的CheckBox，判断是否为Checked
                CheckBox cb = (CheckBox)row.FindControl("cbSingle");
                if (cb.Checked == false)
                {
                    continue;
                }
                // 若为Checked，将信息添加到ArrayList
                selList.Add(Convert.ToInt16(((Label)row.FindControl("lblID")).Text));
                i++;
            }
            // 若遍历完成后ArrayList为空，则表示未选中任何项，页面提示
            if (selList.Count == 0)
            {
                return;
            }
            // 调用逻辑方法，执行删除操作
            for (int j = 0; j < selList.Count; j++)
            {
                pb.UpdateProductShowOnSale(Convert.ToInt16(selList[j]), false);
                //pb.DeleteProduct(Convert.ToInt16(selList[j]));
            }
            // 重新绑定员工列表
            GetGVData(null);
        }

        protected void btnOutput_Click(object sender, EventArgs e)
        {

        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            // 遍历数据集的所有数据项
            foreach (GridViewRow row in this.extGVProduct.Rows)
            {
                // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
                CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
                subCB.Checked = ((CheckBox)sender).Checked;
            }
        }

        protected void extGVProduct_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditItem")
            {
                Response.Redirect("AddProducts.aspx?ProductID=" + e.CommandArgument);
            }
        }

        protected void lbtnPrdtIn_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductIn.aspx");
        }

        protected void lbtnStockOut_Click(object sender, EventArgs e)
        {
            Response.Redirect("StockOut.aspx");
        }

        protected void lbtnPrdtRemark_Click(object sender, EventArgs e)
        {
            Response.Redirect("PrdtRemarkManage.aspx");
        }

        protected void lbtnPrdtAdvisory_Click(object sender, EventArgs e)
        {
            Response.Redirect("Advisory.aspx");
        }

        protected void ddlOtherOperate_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void extGVProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            extGVProduct.PageIndex = e.NewPageIndex;
            GetGVData(null);
        }
}
}
